package engine

import (
	"GoSpider/carwler/fetch"
	"github.com/gomodule/redigo/redis"
	log "github.com/sirupsen/logrus"
	"regexp"
	"time"
)

var redisPool = newPool("192.168.1.190:6382")

func worker(r Request) (ParseResult, error) {
	log.Printf("fetch %s", r.Url)
	body, err := fetch.Fetch(r.Url)
	if err != nil {
		log.Warnf("fetch url %s error %s", r.Url, err.Error())
		return ParseResult{}, err
	}
	return r.ParserFunc(body, r.Url), nil
}

func newPool(server string) *redis.Pool {
	return &redis.Pool{
		MaxIdle:     3,
		IdleTimeout: 240 * time.Second,
		Dial: func() (redis.Conn, error) {
			c, err := redis.Dial("tcp", server)
			if err != nil {
				return nil, err
			}
			return c, err
		},
		TestOnBorrow: func(c redis.Conn, t time.Time) error {
			_, err := c.Do("PING")
			return err
		},
	}
}

var usrRe = regexp.MustCompile(`http://album.zhenai.com/u/[\d]+`)

func visitor(url string) int64 {
	match := usrRe.FindString(url)
	if len(match) != 0 {
		conn := redisPool.Get()
		reply, err := conn.Do("EXISTS", url)
		if err != nil {
			log.Warnf("redis exists url %s error %s", url, err.Error())
		}
		exists := reply.(int64)
		if exists == 0 {
			if _, err := conn.Do("SET", url, url); err == nil {
				return exists
			} else {
				log.Warnf("redis set url %s error %s", url, err.Error())
			}
		}
	}
	return 0
}
